﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using NativeWifi;

namespace Jarvis.Sensors
{
    class WiFiSensors : Sensor
    {
        private Dictionary<String, String> PhoneBook;

        public WiFiSensors()
            : base(10000)
        {
            PhoneBook = new Dictionary<string, string>();
            this.name = "Wireless Network Sensors";
        }

        public override void Sense(Object stateInfo)
        {
            try
            {
                WlanClient wlanClient = new WlanClient();

                foreach (WlanClient.WlanInterface wlanInterface in wlanClient.Interfaces)
                {
                    try
                    {
                        Boolean hasNulls = false;

                        Wlan.WlanConnectionAttributes current = wlanInterface.CurrentConnection;
                        Wlan.WlanAssociationAttributes atts = current.wlanAssociationAttributes;

                        byte[] macAddr = atts.dot11Bssid;
                        var macAddrLen = (uint)macAddr.Length;
                        var str = new string[(int)macAddrLen];
                        for (int i = 0; i < macAddrLen; i++)
                            str[i] = macAddr[i].ToString("x2");

                        string mac = string.Join(":", str);

                        byte[] name = atts.dot11Ssid.SSID;

                        for (int i = 0; i < atts.dot11Ssid.SSIDLength; i++)
                        {
                            if (name[i] == 0)
                                hasNulls = true;
                        }

                        string networkName = null;

                        if (!hasNulls)
                        {
                            networkName = Encoding.UTF8.GetString(name, 0, (int)atts.dot11Ssid.SSIDLength);
                            PhoneBook[mac] = networkName;
                        }

                        if (networkName == null)
                            networkName = "Unidentified Network";

                        Sensor cn = new Sensor("Current Wireless Network");
                        cn.log(networkName + " (" + mac + ")");
                    }
                    catch (Win32Exception)
                    {
                        Sensor cn = new Sensor("Current Wireless Network");
                        cn.log("None");
                    }
                }

                List<object> networks = new List<object>();

                foreach (WlanClient.WlanInterface wlanInterface in wlanClient.Interfaces)
                {
                    Wlan.WlanBssEntry[] wlanBssEntries = wlanInterface.GetNetworkBssList();

                    foreach (Wlan.WlanBssEntry wlanBssEntry in wlanBssEntries)
                    {
                        byte[] macAddr = wlanBssEntry.dot11Bssid;
                        var macAddrLen = (uint)macAddr.Length;
                        var str = new string[(int)macAddrLen];
                        
                        for (int i = 0; i < macAddrLen; i++)
                            str[i] = macAddr[i].ToString("x2");

                        string mac = string.Join(":", str);

                        byte[] name = wlanBssEntry.dot11Ssid.SSID;

                        Boolean hasNulls = false;

                        for (int i = 0; i < wlanBssEntry.dot11Ssid.SSIDLength; i++)
                        {
                            if (name[i] == 0)
                                hasNulls = true;
                        }

                        String networkName = null;

                        if (!hasNulls)
                        {
                            networkName = Encoding.UTF8.GetString(name, 0, (int)wlanBssEntry.dot11Ssid.SSIDLength);
                            PhoneBook[mac] = networkName;
                        }
                        else
                        {
                            try
                            {
                                networkName = PhoneBook[mac];
                            }
                            catch (KeyNotFoundException)
                            {

                            }
                        }

                        if (networkName == null)
                            networkName = "Unidentified Network";

                        networks.Add(networkName + " (" + mac + ")");
                    }

                    Sensor vn = new Sensor("Visible Wireless Networks");
                    vn.log(networks);
                }

                wlanClient = null;
            }
            catch (Exception e)
            {
                Console.WriteLine(" *** wifi exception: " + e);
            }
        }
    }
}
